package com.tmptSource.service.impl;

import com.tmpt.dto.Result;
import com.tmpt.utils.DateTool;
import com.tmpt.service.impl.DataLimitService;
import com.tmptSource.bean.OwIllegalRegister;
import com.tmptSource.bean.OwIssueDeclare;
import com.tmptSource.bean.OwRegisterData;
import com.tmptSource.enums.OwBuildingType;
import com.tmptSource.enums.OwViolationType;
import com.tmptSource.mapper.OwExecuteRecordMapper;
import com.tmptSource.bean.OwExecuteRecord;
import com.tmptSource.dto.OwExecuteRecordDto;
import com.tmptSource.dto.PageDto;
import com.tmptSource.mapper.OwIllegalRegisterMapper;
import com.tmptSource.mapper.OwIssueDeclareMapper;
import com.tmptSource.mapper.OwRegisterDataMapper;
import com.tmptSource.service.intf.IOwExecuteRecordService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tmptSource.utils.dozer.MyMapper;
import com.tmptSource.utils.page.MyPageUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.dozer.Mapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by wxy on 2016/7/28.
 */
@Service(value="owExecuteRecordService")
@Transactional(propagation = Propagation.REQUIRED)
public class OwExecuteRecordService implements IOwExecuteRecordService {

    private Logger log = Logger.getLogger(this.getClass());
    @Autowired private DataLimitService dataLimitService;
    @Autowired private Mapper mapper;
    @Autowired private MyMapper myMapper;
    @Autowired private MyPageUtils myPageUtils;
    @Autowired
    private OwExecuteRecordMapper owExecuteRecordMapper;
    @Autowired
    private OwIssueDeclareMapper owIssueDeclareMapper;
    @Autowired
    private OwIllegalRegisterMapper owIllegalRegisterMapper;
    @Autowired
    private OwRegisterDataMapper owRegisterDataMapper;


    @Override
    public Result<OwExecuteRecordDto> pagination(OwExecuteRecordDto owExecuteRecordDto,PageDto pageDto) {
        //分页查询
        Result<OwExecuteRecordDto> result = myPageUtils.pagination(
                owExecuteRecordDto,pageDto,
                OwExecuteRecord.class,
                OwExecuteRecordDto.class,
                owExecuteRecordMapper,
                false,null);

        if(result.getResultMap().get("rows")!=null) {
            List<OwExecuteRecordDto> owExecuteRecordDtoList = (List<OwExecuteRecordDto>)result.getResultMap().get("rows");
            for (OwExecuteRecordDto owExecuteRecordDto1 : owExecuteRecordDtoList) {
                String businessTableIdStr = owExecuteRecordDto1.getBusinessTableId();
                long businessTableId = Long.parseLong(businessTableIdStr);
                String businessTableName = owExecuteRecordDto1.getBusinessTableName();
                if("ow_issue_declare".equals(businessTableName)){
                    OwIssueDeclare owIssueDeclare = owIssueDeclareMapper.selectById(businessTableId);
                    String type = OwViolationType.explain(owIssueDeclare.getOwViolationType());
                    owExecuteRecordDto1.getMap().put("type",type);
                    owExecuteRecordDto1.getMap().put("number",owIssueDeclare.getIssueNumber());
                    owExecuteRecordDto1.getMap().put("name","问题申报");
                }else if("ow_illegal_register".equals(businessTableName)){
                    OwIllegalRegister owIllegalRegister = owIllegalRegisterMapper.selectById(businessTableId);
                    String type = OwViolationType.explain(owIllegalRegister.getOwViolationType());
                    owExecuteRecordDto1.getMap().put("type",type);
                    owExecuteRecordDto1.getMap().put("number",owIllegalRegister.getIllegalNumber());
                    owExecuteRecordDto1.getMap().put("name","违规处罚");
                }else if("ow_register_data".equals(businessTableName)){
                    OwRegisterData owRegisterData = owRegisterDataMapper.selectById(businessTableId);
                    String type = OwBuildingType.explain(""+owRegisterData.getOwBuildingType());
                    owExecuteRecordDto1.getMap().put("type",type);
                    owExecuteRecordDto1.getMap().put("number",owRegisterData.getRegisterDataNumber());
                    owExecuteRecordDto1.getMap().put("name","信息登记");
                }
            }
        }

        return result;
    }

    /**
    * 分布查询，有数据边界控制功能
    * @param owExecuteRecordDto
    * @param pageDto
    * @param paramMap
    * @return
    */
    @Override
    public Result<OwExecuteRecordDto> pagination(OwExecuteRecordDto owExecuteRecordDto,PageDto pageDto,Map<String,Object> paramMap) {
        //数据边界控制参数
        Map<String,Object> paramMap1 = dataLimitService.getLimitParam(paramMap);

        //分页查询
        Result<OwExecuteRecordDto> result = myPageUtils.pagination(
                owExecuteRecordDto,pageDto,
                OwExecuteRecord.class,
                OwExecuteRecordDto.class,
                owExecuteRecordMapper,
                paramMap1,
                false,null);

        return result;
    }


    @Override
    public Result<OwExecuteRecordDto> selectAll() {
        Result<OwExecuteRecordDto> result = new Result();

        //排序
        PageDto pageDto = new PageDto();
        pageDto.setSortColumn("id");
        pageDto.setSortOrder("desc");
        Map<String,Object> mapParam = new HashMap<>();
        mapParam.put("page",pageDto);

        List<OwExecuteRecord> list = owExecuteRecordMapper.selectAll(mapParam);
        List<OwExecuteRecordDto> dtoList = myMapper.map(list, OwExecuteRecordDto.class);
        result.setResultList(dtoList);

        return result.success();
    }

    @Override
    public Result<OwExecuteRecordDto> selectById(long id) {
        Result<OwExecuteRecordDto> result = new Result();

        OwExecuteRecord owExecuteRecord = owExecuteRecordMapper.selectById(id);
        OwExecuteRecordDto dto = mapper.map(owExecuteRecord,OwExecuteRecordDto.class);
        result.setResultObj(dto);

        return result.success();
    }


    /**
        返回ID值
    */
    @Override
    public Result<OwExecuteRecordDto> addData(OwExecuteRecordDto owExecuteRecordDto){
        Result<OwExecuteRecordDto> result = new Result();

        //封闭数据
        OwExecuteRecord owExecuteRecord = mapper.map(owExecuteRecordDto, OwExecuteRecord.class);
        owExecuteRecord.setCreatedate(DateTool.getCurTimestamp());

        //调用mapper
        long i = owExecuteRecordMapper.insert(owExecuteRecord);
        if(i==0){
            return result.fault("插入失败");
        }

        OwExecuteRecordDto dto = mapper.map(owExecuteRecord,OwExecuteRecordDto.class);
        result.setResultObj(dto);

        return result.success();
    }

    /**
        返回ID值
    */
    @Override
    public Result<OwExecuteRecordDto> editData(OwExecuteRecordDto owExecuteRecordDto){
        Result<OwExecuteRecordDto> result = new Result();

        //封装数据
        OwExecuteRecord owExecuteRecord = mapper.map(owExecuteRecordDto, OwExecuteRecord.class);
        owExecuteRecord.setModifydate(DateTool.getCurTimestamp());

        //调用mapper
        long i = owExecuteRecordMapper.updateById(owExecuteRecord);
        if(i==0){
            return result.fault("编辑数据失败");
        }

        OwExecuteRecordDto dto = mapper.map(owExecuteRecord,OwExecuteRecordDto.class);
        result.setResultObj(dto);

        return result.success();
    }

    /**
    伪删除记录，返回ID值
    */
    @Override
    public Result deleteById(long id,String adminMan){
        Result result = new Result();

        Map<String,Object> mapParameter = new HashMap();
        mapParameter.put("id",id);
        mapParameter.put("modifyman",adminMan);
        mapParameter.put("modifydate",DateTool.getCurTimestamp());

        //调用mapper
        long i = owExecuteRecordMapper.deleteById(mapParameter);
        if(i==0){
            return result.fault("删除数据失败");
        }

        return result.success();
    }





}
